<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Infotech Ltd.
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: vshoor $ ($Date: 2006/04/11 20:58:15 $)
// $RCSfile: index.php,v $ : $Revision: 1.13.71 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================


define("SWIFT_AREA", 20);

if (defined("SWIFTCUSTOMPATH"))
{
	chdir(SWIFTCUSTOMPATH);
} else {
	chdir('./../');
}
require_once ("./swift.php");

if ($_REQUEST["_ca"] == "css")
{
	header("Content-Type: text/css");
	headerCache();

	echo $template->displayTemplate("css.tpl");
	exit;

// ======= ADDED IN V3.00.80 =======
} else if ($_REQUEST["_ca"] == "rebuildcache") {
	// Empty Cache Directory
	if ($handle = opendir('./cache/')) {
		while (false !== ($file = readdir($handle))) { 
			if ($file != "." && $file != ".." && $file != "index.html") { 
				@unlink("./cache/".$file); 
			}
		}

		closedir($handle); 
	}

	// Build the settings cache
	require_once ("./includes/functions_settings.php");
	rebuildSettingsCache();

} else if ($_REQUEST["_ca"] == "login") {
	/*
	* #########################################################
	* Authenticate User, Insert session and redirect else just display login form
	* #########################################################
	*/
	// Remember Password available?
	if (!empty($_POST["cookiepassword"]) && $_POST["password"] == "******")
	{
		// Try to use cookie password
		$staffid = $staffauth->authenticate($_POST["username"], $_POST["cookiepassword"], false, false);
	} else {
		$staffid = $staffauth->authenticate($_POST["username"], $_POST["password"], false);
	}

	if (!empty($staffid))
	{
		require_once ("./includes/functions_html.php");

		/* Authentication successfull, Generate a sessionid for this user*/
		$session->insertSession(SESSION_STAFF, $staffid);

		// We always empty the remember cookie if username differs
		$_cookieusername = $cookie->getSCookie("remusername");
		if ($_POST["username"] != $_cookieusername && !empty($_cookieusername))
		{
			$cookie->emptySCookie("remusername");
			$cookie->emptySCookie("rempassword");
		}

		if ($_POST["remember"] == 1 && $_POST["password"] != "******")
		{
			$cookie->setSCookie("remusername", $_POST["username"], true);
			$cookie->setSCookie("rempassword", md5($_POST["password"]), true);
		} else if ($_POST["remember"] == 1 && $_POST["password"] == "******") {
			// Do Nothing
		} else {
			$cookie->emptySCookie("remusername");
			$cookie->emptySCookie("rempassword");
		}

		if (!empty($_POST["querystring"]))
		{
			printRedirect($_SWIFT["language"]["redirect_login"], "index.php?".$_POST["querystring"], true);
		} else {
			printRedirect($_SWIFT["language"]["redirect_login"], "index.php?_m=core&_a=dashboard", true);
		}

	} else {
		$cookie->emptySCookie("remusername");
		$cookie->emptySCookie("rempassword");

		$staffauth->displayLoginForm($_SWIFT["errorstr"]);
	}
} else if ($_REQUEST["_ca"] == "logout") {
	$session->logoutSession(SESSION_STAFF);
	$cookie->emptyCookie("staffsettings");

	$staffauth->displayLoginForm($_SWIFT["language"]["loggedout"]);
} else {
	if (!$session->start(SESSION_STAFF)) {
		/*
		* #########################################################
		* No other action, we always try to start a session
		* #########################################################
		*/
		// Failed to load session
		$staffauth->displayLoginForm($_SWIFT["errorstr"]);
		exit;
	}

	$cookie->parseCookie("staffsettings");

	if ($_SWIFT["staff"]["pmunread"] > 0 && $_GET["popupshow"] != 1 && $cookie->getKey("staffsettings", "prvpopup") != 1 && $_SWIFT["staff"]["enablepmjsalerts"] == 1)
	{
		$cookie->addCookie("staffsettings", "prvpopup", "1");
		$cookie->buildCookie("staffsettings", true);

		$template->assign("prvmsgshow", true);
	}

	// User logged in, trigger events
	if (!$events->triggerEvent(EVENT_STAFF))
	{
		require_once ("./includes/functions_html.php");
		printRedirect($_SWIFT["language"]["redirect_dashboard"], "index.php?_m=core&_a=dashboard", true);
	}
}

$_SWIFT["endtime"] = fetchmicrotime();

//$dbCore->debugOutput();
//echo "Total Time Taken: ".($_SWIFT["endtime"]-$_SWIFT["starttime"]) ." seconds";
?>